package cn.net.lwj.singledb.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 项目启动后打开浏览器
 * spring 项目启动时，可以有很多方法，让它执行下一个东西，比如ServletContainerInitializer ，监听容器生命周期的ServletContextListener ，还有CommandLineRunner ，该接口是springboot 自己，在项目启动后会自动扫描CommandLineRunner 实现类中的run 方法，并执行
 * 即cmd调用一个浏览器去打开页面
 * 该类放置的位置，比springboot 启动类低一级即可
 * String cmd = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe http://localhost:8080";
 * String cmd = "cmd   /c   start   http://localhost:8080/index";  两个地址都行
 */
@Order(value = 1)
//@Component//这个注解必须要,把该类实例化到spring容器中
public class BrowserStartRunner implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(BrowserStartRunner.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("---------------------浏览器");
        //String googleExcutePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
        String loginUrl = "http://localhost:8071/demo";
        //Runtime.getRuntime().exec("cmd   /c   start   http://localhost:8080/index");
        //String cmd = googleExcutePath + " " + loginUrl;
        String cmd = "cmd   /c   start " + loginUrl;
        Runtime run = Runtime.getRuntime();
        try {
            run.exec(cmd);
            logger.debug("启动浏览器打开项目成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
}
